package com.nokia.examplegame;


import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class GameMIDlet extends MIDlet implements CommandListener {

	private Display display;
	private SplashScreenView splashScreen;
	private GameCanvasView gameCanvas;
	private Command exitCmd = new Command("Exit", Command.EXIT, 0);
	private Command playCmd = new Command("Play", Command.SCREEN, 0);
	private Command playAgainCmd = new Command("Play Again", Command.SCREEN, 0);
	private Command pauseCmd = new Command("Pause", Command.SCREEN, 0);
	
	private static String SPLASH_1 = "splashscreen/companyimage.png";
	private static String SPLASH_2 = "splashscreen/splashimage.png";
	private static String GAME_BACKGROUND = "game/background.png";
	private static String GAME_PLAYER = "game/player.png";
	private static String GAME_OBSTACLE = "game/obstacle.png";
	private static String GAME_BACKGROUNDLAYER = "game/backgroundlayer.png";
	private static String FRAMEWORK_INSTRUCTIONS = "framework/instructions.png";
	private static String FRAMEWORK_PAUSED = "framework/paused.png";
	private static String FRAMEWORK_GAMEOVER = "framework/gameover.png";
	private static int TIMEOUT = 4000;
	
	public GameMIDlet() {
		this.display = Display.getDisplay(this);
		
		this.gameCanvas = new GameCanvasView();
        int width = gameCanvas.getWidth();
        int height = gameCanvas.getHeight();
        Image[] images = new Image[2];
		images[0] = scaleImage(loadImage(SPLASH_1), width, height);
		images[1] = scaleImage(loadImage(SPLASH_2), width, height);
		Image gameBackground = scaleImage(loadImage(GAME_BACKGROUND), width, height);
		Image gamePlayer = loadImage(GAME_PLAYER);
		Image gameObstacle = loadImage(GAME_OBSTACLE);
		Image gameBackgroundLayer = loadImage(GAME_BACKGROUNDLAYER);
		Image frameworkPaused = loadImage(FRAMEWORK_PAUSED);
		Image frameworkGameOver = loadImage(FRAMEWORK_GAMEOVER);
		Image frameworkInstructions = loadImage(FRAMEWORK_INSTRUCTIONS);
        this.gameCanvas.createCanvas(this, frameworkInstructions, frameworkPaused, frameworkGameOver, gameBackground, gamePlayer, gameObstacle, gameBackgroundLayer);
		this.gameCanvas.addCommand(playCmd);
		this.gameCanvas.addCommand(exitCmd);
		this.gameCanvas.setCommandListener(this);
		this.splashScreen = new SplashScreenView(display, gameCanvas, images, TIMEOUT);
	}
	
	private Image loadImage(String image) {
		Image splashImage = null;
		try {
			splashImage = Image.createImage("/" + image);
		} catch (Exception e) {
			System.out.println("Couldn't load image " + image);
		}
		return splashImage;
	}

	protected void startApp() throws MIDletStateChangeException {
		this.display.setCurrent(splashScreen);
		
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		this.gameCanvas.stopGame();
		this.gameCanvas = null;
		this.splashScreen  = null;
		this.display = null;
		
	}

	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == playCmd) {
			this.gameCanvas.playGame();
			this.gameCanvas.removeCommand(playCmd);
			this.gameCanvas.addCommand(pauseCmd);
		} else if (cmd == pauseCmd) {
			this.gameCanvas.pauseGame();
			this.gameCanvas.removeCommand(pauseCmd);
			this.gameCanvas.addCommand(playCmd);
		} else if (cmd == playAgainCmd) {
			this.gameCanvas.reset();
			this.gameCanvas.addCommand(playCmd);
			this.gameCanvas.removeCommand(playAgainCmd);
		} else if (cmd == exitCmd){
			notifyDestroyed();
		} 
	}


	public void gameOver() {
		this.gameCanvas.removeCommand(pauseCmd);
		this.gameCanvas.addCommand(playAgainCmd);
	}
    public static Image scaleImage(Image original, int newWidth, int newHeight) {
    int[] rawInput = new int[original.getHeight() * original.getWidth()];
    original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());

    int[] rawOutput = new int[newWidth * newHeight];

    // YD compensates for the x loop by subtracting the width back out
    int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth();
    int YR = original.getHeight() % newHeight;
    int XD = original.getWidth() / newWidth;
    int XR = original.getWidth() % newWidth;
    int outOffset = 0;
    int inOffset = 0;

    for (int y = newHeight, YE = 0; y > 0; y--) {
        for (int x = newWidth, XE = 0; x > 0; x--) {
            rawOutput[outOffset++] = rawInput[inOffset];
            inOffset += XD;
            XE += XR;
            if (XE >= newWidth) {
                XE -= newWidth;
                inOffset++;
            }
        }
        inOffset += YD;
        YE += YR;
        if (YE >= newHeight) {
            YE -= newHeight;
            inOffset += original.getWidth();
        }
    }
    return Image.createRGBImage(rawOutput, newWidth, newHeight, false);
    }

}
